package com.zf.zfed.audit.ui

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.widget.ScrollView
import androidx.core.content.ContextCompat
import com.zf.zfed.audit.R

/**
 * 自定义ScrollView，支持自定义滚动条样式
 */
class CustomScrollView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : ScrollView(context, attrs, defStyleAttr) {

    init {
        // 设置滚动条样式
        setupScrollbar()
    }

    private fun setupScrollbar() {
        // 设置垂直滚动条
        isVerticalScrollBarEnabled = true
        scrollBarStyle = SCROLLBARS_OUTSIDE_OVERLAY
        isScrollbarFadingEnabled = false
        
        // 设置滚动条颜色
        try {
            val thumbDrawable = ContextCompat.getDrawable(context, R.drawable.scrollbar_thumb)
            val trackDrawable = ContextCompat.getDrawable(context, R.drawable.scrollbar_track)
            
            // 使用反射设置滚动条样式（Android API 29+）
            val scrollViewClass = ScrollView::class.java
            try {
                val thumbField = scrollViewClass.getDeclaredField("mScrollBarThumb")
                thumbField.isAccessible = true
                thumbField.set(this, thumbDrawable)
                
                val trackField = scrollViewClass.getDeclaredField("mScrollBarTrack")
                trackField.isAccessible = true
                trackField.set(this, trackDrawable)
            } catch (e: Exception) {
                // 如果反射失败，使用默认样式
                e.printStackTrace()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}
